﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YRevitJig;

namespace YArchitech.Plumbing
{
	internal class SprinklerArcJig : XUiJigBase
	{
		public SprinklerArcJig(UIDocument uiDoc) : base(uiDoc)
		{
		}

		public bool ArcPoint(SectorLaySprinkler sec, UserSettedInfo set)
		{
			bool result;
			try
			{
				this.sector = sec;
				this.userSet = set;
				this.selPos = SprinklerArcJig.SelPointPos.eStart;
				this.selPos = SprinklerArcJig.SelPointPos.eStart;
				this.sector.sPoint = base.UiDoc.Selection.PickPoint("选择起点……");
				this.selPos = SprinklerArcJig.SelPointPos.eEnd;
				this.sector.ePoint = base.UiDoc.Selection.PickPoint("选择终点……");
				this.selPos = SprinklerArcJig.SelPointPos.eOn;
				this.sector.onPoint = base.UiDoc.Selection.PickPoint("弧线上一点……");
				string message = "";
				bool flag = this.sector.CheckPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, ref message);
				while (!flag)
				{
					YJKMessageBox.Show(message);
					this.selPos = SprinklerArcJig.SelPointPos.eStart;
					this.sector.sPoint = base.UiDoc.Selection.PickPoint("选择起点……");
					this.selPos = SprinklerArcJig.SelPointPos.eEnd;
					this.sector.ePoint = base.UiDoc.Selection.PickPoint("选择终点……");
					this.selPos = SprinklerArcJig.SelPointPos.eOn;
					this.sector.onPoint = base.UiDoc.Selection.PickPoint("弧线上一点……");
					message = "";
					flag = this.sector.CheckPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, ref message);
				}
				this.selPos = SprinklerArcJig.SelPointPos.eNo;
				this.GetPoints();
				result = true;
			}
			catch
			{
				this.selPos = SprinklerArcJig.SelPointPos.eNo;
				result = false;
			}
			return result;
		}

		public override void Draw(PaintEventArgs paintEventArgs)
		{
			base.Draw(paintEventArgs);
			System.Drawing.Point ptCur = this.m_ptCur;
			if (this.m_ptCur.IsEmpty || paintEventArgs == null)
			{
				return;
			}
			if (this.selPos == SprinklerArcJig.SelPointPos.eNo || this.selPos == SprinklerArcJig.SelPointPos.eStart || this.sector == null || this.userSet == null)
			{
				return;
			}
			try
			{
				XYZ xyz = base.Client2Revit(this.m_ptCur);
				if (xyz != null)
				{
					XGraphics g = new XGraphics(paintEventArgs.Graphics, base.JigPen);
					if (this.selPos == SprinklerArcJig.SelPointPos.eEnd)
					{
						new XLine2D(base.Revit2Client(this.sector.sPoint), this.m_ptCur).Draw(g);
					}
					else if (this.selPos == SprinklerArcJig.SelPointPos.eOn)
					{
						System.Drawing.Point ptStart = base.Revit2Client(this.sector.sPoint);
						System.Drawing.Point ptEnd = base.Revit2Client(this.sector.ePoint);
						this.sector.onPoint = xyz;
						System.Drawing.Point ptCur2 = this.m_ptCur;
						if (!this.sector.sPoint.IsAlmostEqualTo(this.sector.ePoint, 0.001) && !xyz.IsAlmostEqualTo(this.sector.sPoint, 0.001) && !xyz.IsAlmostEqualTo(this.sector.ePoint, 0.001))
						{
							Graphics graphics = paintEventArgs.Graphics;
							new XArc2D(ptStart, ptEnd, ptCur2).Draw(g);
							this.GetPoints();
							foreach (XYZ point in this.xyzList)
							{
								new XCircle2D(base.Revit2Client(point), this.nRadius).Draw(g);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ex.ToString();
			}
		}

		private void GetPoints()
		{
			try
			{
				double offset = this.userSet.OffSet * 1000.0 / 304.8;
				if (this.userSet.LimitedArc == "distance")
				{
					int arcDistance = this.userSet.ArcDistance;
					int arcSide = this.userSet.ArcSide;
					this.xyzList = this.sector.GetArcLayPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, arcDistance, arcSide, offset);
				}
				else
				{
					int arcNum = this.userSet.ArcNum;
					double arcRatio = this.userSet.ArcRatio;
					this.xyzList = this.sector.GetArcLayPoints(this.sector.sPoint, this.sector.ePoint, this.sector.onPoint, arcNum, arcRatio, offset);
				}
			}
			catch
			{
				this.xyzList = new List<XYZ>();
			}
		}

		private int nRadius = 3;

		private SprinklerArcJig.SelPointPos selPos;

		private SectorLaySprinkler sector;

		private UserSettedInfo userSet;

		public List<XYZ> xyzList = new List<XYZ>();

		private enum SelPointPos
		{
			eNo,
			eStart,
			eEnd,
			eOn
		}
	}
}
